# Dockerfile for Stable Video Diffusion
#
# Author: Your Name
# Date: 2025-09-05

# --- Base Image ---
# 使用官方的 PyTorch 镜像，包含 CUDA 和 cuDNN
FROM pytorch/pytorch:2.1.0-cuda12.1-cudnn8-runtime

# --- Metadata ---
LABEL maintainer="Your Name <your.email@example.com>"
LABEL description="Stable Video Diffusion (SVD) Inference Server"

# --- Environment Variables ---
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV PATH="/app/svd_env/bin:$PATH"
ENV HF_HOME="/app/models"
ENV MPLCONFIGDIR="/app/config/matplotlib"

# --- System Dependencies ---
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    git \
    curl \
    ffmpeg \
    libsm6 \
    libxext6 \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# --- Application Setup ---
WORKDIR /app

# 创建Python虚拟环境
RUN python3 -m venv svd_env

# 复制依赖和配置文件
COPY requirements.txt config/ ./ 

# 安装Python依赖
# 确保在虚拟环境中执行
RUN . svd_env/bin/activate && \
    pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# 复制应用代码
COPY . .

# --- Port Exposure ---
EXPOSE 8080

# --- Healthcheck ---
# 增加健康检查，方便容器编排工具监控
HEALTHCHECK --interval=30s --timeout=10s --start-period=300s --retries=3 \
  CMD curl -f http://localhost:8080/health || exit 1

# --- Entrypoint ---
# 使用 gunicorn 启动服务，性能更佳
CMD ["sh", "-c", " . svd_env/bin/activate && gunicorn svd_server:app -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8080 --timeout 300"]

# --- Pre-run Steps (in docker-compose) ---
# 1. 下载模型: 
#    - docker-compose run --rm svd_service python download_models.py
# 2. 启动服务:
#    - docker-compose up -d

